<div class="page-header">
	<h1>
		Mercado <small>novo</small>
	</h1>
</div>

<div class="row">
	<div>
		<ul class="breadcrumb">
			<li><?php echo $this->href(array('controller' => 'index', 'action' => 'index', 'module' => 'default', 'content' => 'Home', 'checkAcl' => true))?></li>
			<span class="text-info divider"> >> </span>
			<li><?php echo $this->href(array('controller' => 'mercado', 'action' => 'add-mercado-preferido', 'module' => 'default', 'content' => 'Mercados Preferidos', 'checkAcl' => true))?></li>
			<span class="text-info divider"> >> </span>
			<li class="active">Criar Novo Mercado</li>
		</ul>
	</div>
</div>

<div class="row">
	<div class="col-md-8 col-md-offset-2">
		<div class="panel panel-info">
			<div class="panel-heading">
				<div class="panel-title">Criar novo mercado</div>
			</div>

			<div style="padding-top: 30px" class="panel-body">
				<form class="form-horizontal" role="form">
					<fieldset>

						<!-- Form Name -->
						<legend>Detalhes</legend>

						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label required" for="textinput">Rede</label>
							<div class="col-sm-10">
								<input type="text" id="nm_rede_mercado" name="nm_rede_mercado" placeholder="Ex: Carrefour" class="form-control" required>
							</div>
						</div>

						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label required" for="textinput">Nome</label>
							<div class="col-sm-10">
								<input type="text" id="nm_mercado" name="nm_mercado" placeholder="Ex: Carrefour Tijuca" class="form-control" required>
							</div>
						</div>

						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label" for="textinput">CEP</label>
							<div class="col-sm-4">
								<input type="text" name="nr_cep" id="nr_cep" placeholder="CEP" class="form-control">
							</div>
						</div>
						
						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label" for="textinput">Endere&ccedil;o</label>
							<div class="col-sm-10">
								<input type="text" id="nm_logradouro" name="nm_logradouro" placeholder="Ex: Rua S&atilde;o Miguel, Avenida Maracan&atilde;" class="form-control">
							</div>
						</div>
                                                

						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label" for="textinput">N&uacute;mero</label>
							<div class="col-sm-2">
								<input type="number" min="1" placeholder="Ex: 1831" id="nr_numero" name="nr_numero" maxlength="6" size="6" class="form-control">
							</div>

							<label class="col-sm-2 control-label" for="textinput">Compl.</label>
							<div class="col-sm-6">
								<input type="text" placeholder="Ex: Loja A" name="ds_complemento" class="form-control">
							</div>
						</div>


						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label required" for="textinput">Bairro</label>
							<div class="col-sm-10">
								<input type="text" id="nm_bairro" name="nm_bairro" placeholder="Ex: Tijuca" class="form-control" required>
							</div>
						</div>
						
						
						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label required" for="textinput">UF</label>
							<div class="col-sm-2">
								<select name="sg_uf" id="sg_uf" class="form-control">
									<option value="AC">AC</option>
									<option value="AL">AL</option>
									<option value="AM">AM</option>
									<option value="AP">AP</option>
									<option value="BA">BA</option>
									<option value="CE">CE</option>
									<option value="DF">DF</option>
									<option value="ES">ES</option>
									<option value="GO">GO</option>
									<option value="MA">MA</option>
									<option value="MG">MG</option>
									<option value="MS">MS</option>
									<option value="MT">MT</option>
									<option value="PA">PA</option>
									<option value="PB">PB</option>
									<option value="PE">PE</option>
									<option value="PI">PI</option>
									<option value="PR">PR</option>
									<option value="RJ" selected>RJ</option>
									<option value="RN">RN</option>
									<option value="RO">RO</option>
									<option value="RR">RR</option>
									<option value="RS">RS</option>
									<option value="SC">SC</option>
									<option value="SE">SE</option>
									<option value="SP">SP</option>
									<option value="TO">TO</option>
								</select>
							</div>

							<label class="col-sm-2 control-label required" for="textinput">Cidade</label>
							<div class="col-sm-6">
								<input type="text" name="nm_cidade" id="nm_cidade" placeholder="Ex: Rio de Janeiro, S&atilde;o Paulo" class="form-control" required>
							</div>
						</div>
												

						<!-- Text input-->
						<div class="form-group">
							<label class="col-sm-2 control-label" for="textinput">Latitude</label>
							<div class="col-sm-4">
                                                            <input type="text" id="vl_latitude" name="vl_latitude" placeholder="Latitude" class="form-control" readonly>
							</div>
                                                        <label class="col-sm-2 control-label" for="textinput">Longitude</label>
                                                        <div class="col-sm-4">
                                                            <input type="text" id="vl_longitude" name="vl_longitude" placeholder="Longitude" class="form-control" readonly>
                                                        </div>
						</div>
                                                
                                                
						<div class="form-group">
							<div class="col-sm-offset-2 col-sm-10">
								<div class="pull-right">
									<button type="submit" class="btn btn-default">Cancelar</button>
									<button type="submit" class="btn btn-primary">Salvar</button>
								</div>
							</div>
						</div>

					</fieldset>
				</form>
			</div>

		</div>

	</div>
	<!-- /.col-lg-12 -->
</div>
<!-- /.row -->


<script type="text/javascript" src="<?php echo $this->baseUrl()?>/public/js/jquery.js"></script>
<script>
    /* preencher latitude longitude criacao mercado */
    $("#nm_logradouro").blur(function(){callGoogleMaps()});
    $("#nr_numero").blur(function(){callGoogleMaps()});
    $("#sg_uf").blur(function(){callGoogleMaps()});
    $("#nm_cidade").blur(function(){callGoogleMaps()});

    function callGoogleMaps(){

            var address = $("#nm_logradouro").val() + ',' + $("#nr_numero").val() + ',' + $("#nm_bairro").val()  + ',' + $("#nm_cidade").val() + ',' + $("#sg_uf").val();
            address = address.replace(" ", "+");

            $.getJSON("http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=true_or_false",
                function(j){
                    $("#vl_latitude").val(j['results'][0]['geometry']['location']['lat']);
                    $("#vl_longitude").val(j['results'][0]['geometry']['location']['lng']);

                    $(j['results'][0]['address_components']).each(function(){
                        if (this['types'][0] == 'postal_code')
                            $("#nr_cep").val(this['long_name']);
                    });
            });  
    }
    /* preencher latitude longitude criacao mercado */
</script>